package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewCskuclrCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCskuMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xuxinshuai
 * @date 2017/10/20
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class ViewCskucrlCmdImpl extends CommandAdapter implements ViewCskuclrCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewCskuMapper viewCskuMapper = ApplicationContextHandle.getBean(ViewCskuMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || param.size() == 0|| param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
//        log.debug("----------颜色查询参数----------"+param);
        log.info("ViewCskucrlCmdImpl--param-->{}",param);

        List<HashMap> mapList = new ArrayList<>();
        mapList = viewCskuMapper.selectskuclrsql(param.getJSONArray("IDS").toArray());
        JSONArray jsonArray = listToJsonarray(mapList);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // 商品
            if (jsonObject.getString("PS_C_PRO_ID") != null) {
                long cproid = jsonObject.getLong("PS_C_PRO_ID");
                String cproecode = viewCskuMapper.selectproecode(cproid);
                jsonObject.put("PS_C_PRO_ID", cproecode);
            }
            //TODO  跨库查询
            //配销中心
            if (jsonObject.getString("CP_C_DISTRIB_ID") != null) {
                long storeid = jsonObject.getLong("CP_C_DISTRIB_ID");
                String storecode = viewCskuMapper.selectstoreecode(storeid);
                jsonObject.put("CP_C_DISTRIB_ID", storecode);
            }
            //颜色
            if (jsonObject.getString("PS_C_SPEC1OBJ_ID") != null) {

                Long specobjid = jsonObject.getLong("PS_C_SPEC1OBJ_ID");
                String ecodeStr = viewCskuMapper.selectspececood(specobjid);
                jsonObject.put("CRL_ECODE", ecodeStr);

            }else {

                jsonObject.put("CRL_ECODE","");
            }
            jsonObject.remove("PS_C_SPEC1OBJ_ID");
            jsonObject.remove("PS_C_SPEC2OBJ_ID");
            //创建人
            //TODO  跨库查询
            if (jsonObject.getString("OWNERID") != null) {
                long storeid = jsonObject.getLong("OWNERID");
                String userename = viewCskuMapper.selectusername(storeid);
                jsonObject.put("OWNERID", userename);
            }
            //翻译主副色
            if (jsonObject.getString("MAINCOLOR") != null) {
                String maincolor = jsonObject.getString("MAINCOLOR");
                if ("1".equals(maincolor)) {
                    jsonObject.put("MAINCOLOR", "A");
                } else if ("2".equals(maincolor)) {
                    jsonObject.put("MAINCOLOR", "B");
                } else if ("3".equals(maincolor)) {
                    jsonObject.put("MAINCOLOR", "C");
                }
            } else {
                jsonObject.put("MAINCOLOR", "B");
            }
            //修改人
            if (jsonObject.getString("MODIFIERID") != null) {
                long userid = jsonObject.getLong("MODIFIERID");
                String ename = viewCskuMapper.selectusername(userid);
                jsonObject.put("MODIFIERID", ename);
            }
        }
        vh.put("data", jsonArray);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("success"));
        return vh;
    }

    /**
     * list转jsonarray
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }
}
